package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.PermissionSingleDao;
import org.example.Dao.RPDao;
import org.example.Pojo.Permission;
import org.example.Pojo.PermissionSingle;
import org.example.Pojo.RP;
import org.example.Service.PermissionSingleService;
import org.example.Utils.UnionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Auto created by codeAppend plugin
 */

@Service
public class PermissionSingleServiceImpl implements PermissionSingleService {
    @Resource
    RPDao rpDao;
    @Resource
    private PermissionSingleDao permissionSingleDao;

    @Override
    public UnionResult GetAllPermissions() {
        QueryWrapper<PermissionSingle> queryWrapper = new QueryWrapper<>();
        return UnionResult.Success("",permissionSingleDao.selectList(queryWrapper));
    }

    @Override
    public UnionResult AddPermission(PermissionSingle permissionSingle) {
        if (permissionSingleDao.insert(permissionSingle) > 0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult DeletePermission(PermissionSingle permissionSingle) {
        QueryWrapper<PermissionSingle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",permissionSingle.getUid());
        QueryWrapper< RP> rpQueryWrapper  = new QueryWrapper<>();
        rpQueryWrapper.eq("permission_id",permissionSingle.getUid());
        if (rpDao.selectCount(rpQueryWrapper) > 0){
            return UnionResult.Failure("该权限正在被角色使用");
        }
        if (permissionSingleDao.delete(queryWrapper) > 0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult UpdatePermission(PermissionSingle permissionSingle) {
        if (permissionSingleDao.updateById(permissionSingle) > 0){
            return UnionResult.Success();
        }else {
            return UnionResult.Failure();
        }
    }
}